package test;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.aierjun.sqlaitivity.DBHelper;
import com.aierjun.sqlaitivity.R;
import com.aierjun.sqlaitivity.TestActivity;

public class MyActivity extends Activity
{
    private Button btn;
    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        tv = (TextView) findViewById(R.id.tv);
        btn.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v) {
                update();
            }
        });
    }
    private void update(){
        UpdateTextTask updateTextTask = new UpdateTextTask(this);
        updateTextTask.execute();
    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        private Context context;
        UpdateTextTask(Context context) {
            this.context = context;
        }
        /**
         * 运行在UI线程中，在调用doInBackground()之前执行
         */
        @Override
        protected void onPreExecute() {
            Toast.makeText(context,"开始执行",Toast.LENGTH_SHORT).show();
        }
        /**
         * 后台运行的方法，可以运行非UI线程，可以执行耗时的方法
         */
        public String str;
        @Override
        protected Integer doInBackground(Void... params) {
           str="我是后台执行的内容";
            Log.d("JunLog","后台运行");
            int i=0;
            while(i<10){
                i++;
                publishProgress(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
            return null;
        }

        /**
         * 运行在ui线程中，在doInBackground()执行完毕后执行
         */
        @Override
        protected void onPostExecute(Integer integer) {
            Toast.makeText(context,"执行完毕",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent();
            intent.setClass(MyActivity.this,TestActivity.class);
            intent.putExtra("haha",str);
            startActivity(intent);
        }

        /**
         * 在publishProgress()被调用以后执行，publishProgress()用于更新进度
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
            tv.setText(""+values[0]+"0%");
        }
    }
}

